/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.action.manage;

import org.zhiqim.account.cache.SensitiveWordCache;
import org.zhiqim.account.dbo.sys.ZacSensitiveWord;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.paging.PageResult;
import org.zhiqim.kernel.util.Ids;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Updater;

public class SensitiveWordAction extends StdSwitchAction
{

    @Override
    protected void validateId(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("wordId", "词汇编号不能为空"));
    }

    @Override
    protected void validateForm(HttpRequest request) throws Exception
    {
        request.addValidate(new IsNotEmpty("wordContent", "词汇内容不能为空"));
    }

    @Override
    protected void list(HttpRequest request) throws Exception
    {
        int page = request.getParameterInt("page", 1);
        int pageSize = request.getContextAttributeInt("zmr_page_size", 20);
        
        PageResult<ZacSensitiveWord> result = Global.get(ZTable.class).page(ZacSensitiveWord.class, page, pageSize);
        result.addConditionMap(request.getParameterMap());
        
        request.setAttribute("result", result);
    }

    @Override
    protected void add(HttpRequest request) throws Exception
    {
        
    }

    @Override
    protected void modify(HttpRequest request) throws Exception
    {
        long wordId = request.getParameterLong("wordId");
        ZacSensitiveWord item = Global.get(ZTable.class).item(ZacSensitiveWord.class, wordId);
        if (item == null)
        {
            request.returnHistory("该词汇不存在");
            return;
        }
        
        request.setAttribute("item", item);
    }

    @Override
    protected void insert(HttpRequest request) throws Exception
    {
        ZacSensitiveWord word = new ZacSensitiveWord();
        word.setWordId(Ids.longId13());
        word.setWordContent(request.getParameter("wordContent"));
        word.setWordType(request.getParameterInt("wordType"));
        
        Global.get(ZTable.class).insert(word);
        Global.get(SensitiveWordCache.class).doRefresh();
    }

    @Override
    protected void update(HttpRequest request) throws Exception
    {
        Updater updater = new Updater();
        updater.addMust("wordId", request.getParameterLong("wordId"));
        updater.addField("wordType", request.getParameterInt("wordType"));
        updater.addField("wordContent", request.getParameter("wordContent"));
        
        Global.get(ZTable.class).update(ZacSensitiveWord.class, updater);
        Global.get(SensitiveWordCache.class).doRefresh();
    }

    @Override
    protected void delete(HttpRequest request) throws Exception
    {
        Global.get(ZTable.class).delete(ZacSensitiveWord.class, request.getParameterLong("wordId"));
        Global.get(SensitiveWordCache.class).doRefresh();
    }

}
